package com.restaurant.application.minifacade;

import com.google.common.base.Strings;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.restaurant.application.minifacade.support.AbstractMiniFacade;
import com.restaurant.application.minifacade.support.PathData;
import com.restaurant.application.minifacade.support.RequestData;
import com.restaurant.application.util.CommonUtils;

import java.util.stream.Stream;

public class MiniFacadeImpl extends AbstractMiniFacade {

    @Override
    public View getFullView() {
        PathData pathData = new PathData();
        pathData.withActionPath(getPath(CMD_ACTION));
        return CommonUtils.resultView(this, "success", pathData);
    }

    @Override
    public View getViewLead(ViewSpec spec) {
        return null;
    }

    @Override
    public ViewUpdate action(RequestData data, int[] seq) {
        String action = data.getAction();
        if (Strings.isNullOrEmpty(action)) {
            return returnView(getFullView());
        }
        String json = data.getData();
        return returnView(processAction(action, json));
    }

    private View processAction(String action, String json) {
        if (Stream.of(Action.values()).noneMatch(e -> e.name().equals(action))) {
            return getFullView();
        }
        View view;
        ActionController controller = new ActionController(this);
        switch (Action.valueOf(action)) {
            case registerOrNot:
                view = controller.doRegister(json);
                break;
            case verify:
                view = controller.doVerify(json);
                break;
            case mainPageInfo:
                view = controller.doMainPageInfo(json);
                break;
            case login:
                view = controller.doLogin(json);
                break;
            case getInformation:
                view = controller.doGetInformation(json);
                break;
            case informationCommit:
                view = controller.doInformationCommit(json);
                break;
            case getSignIn:
                view = controller.doGetSignIn(json);
                break;
            case getShopNameList:
                view = controller.doGetShopNameList(json);
                break;
            case myReservationCommit:
                view = controller.doMyReservationCommit(json);
                break;
            case getMyReservationList:
                view = controller.doGetMyReservationList(json);
                break;
            case getMyReservationDetail:
                view = controller.doGetMyReservationDetail(json);
                break;
            case cancelReservation:
                view = controller.doCancelReservation(json);
                break;
            case getConsumeHistory:
                view = controller.doGetConsumeHistory(json);
                break;
            case getCardList:
                view = controller.doGetCardList(json);
                break;
            case getMyCardList:
                view = controller.doGetMyCardList(json);
                break;
            case getMyCardListByStatus:
                view = controller.doGetMyCardListByStatus(json);
                break;
            case getCardDetail:
                view = controller.doGetCardDetail(json);
                break;
            case getGoodsList:
                view = controller.doGetGoodsList(json);
                break;
            case getGoodsDetail:
                view = controller.doGetGoodsDetail(json);
                break;
            case quickChange:
                view = controller.doQuickChange(json);
                break;
            case searchGoods:
                view = controller.doSearchGoods(json);
                break;
            case getShopAddress:
                view = controller.doGetShopAddress(json);
                break;
            case changePhone:
                view = controller.doChangePhone(json);
                break;
            case getBookingDate:
                view = controller.doGetBookingDate(json);
                break;
            default:
                view = getFullView();
        }
        return view;
    }
}
